//////////////////////////////////////////////////////////////////////
//
//	Copyright 2010-2011 Rogo's Team. All Rights Reserved
//
//////////////////////////////////////////////////////////////////////
package com.rogo.weibo.platform.netease.api.favorite
{
	import com.rogo.weibo.core.DoAPI;
	import com.rogo.weibo.platform.netease.api.oauth.Oauth;
	import com.rogo.weibo.utils.HttpParameter;
	import com.rogo.weibo.utils.OauthUrlUtil;
	
	import flash.net.URLRequestMethod;
	
	import json.JSON;
	
	import mx.utils.StringUtil;
	
	/**
	 * 网易微博API 
	 * 1.1.9 收藏
	 * 
	 * @see http://rogo.sinaapp.com
	 * @see http://open.t.163.com
	 * @author rogo
	 */	
	public class DoFavorite extends DoAPI
	{
		public function DoFavorite(dataHandler:Function, errorHandler:Function)
		{
			super(dataHandler, errorHandler);
		}
		
		//--------------------------------------------------------------------------
		//
		//  Constants
		//
		//--------------------------------------------------------------------------
		
		private static const URL_FAVORITES:String = 'http://api.t.163.com/favorites/{0}.json';
		public static const CMD_FAVORITES:String = 'cmdFavorites';
		
		private static const URL_ADD_FAVORITES:String = 'http://api.t.163.com/favorites/create/id.json';
		public static const CMD_ADD_FAVORITES:String = 'cmdAddFavorites';
		
		private static const URL_DESTROY_FAVORITES:String = 'http://api.t.163.com/favorites/destroy/id.json';
		public static const CMD_DESTROY_FAVORITES:String = 'cmdDestroyFavorites';
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		/**
		 * 获取指定用户的收藏列表
		 * 
		 * @param id 必选参数，可以为该用户的个性网址(screen_name)
		 * @param count	返回微博数量，默认30，最大200
		 * @param since_id 分页参数，传cursor_id,返回此条微博以前发的微博列表,不包含此条
		 */		
		public function getFavorites(id:String, count:String, since_id:String):void
		{
			var paras:Array = [];
			var favURL:String = StringUtil.substitute(URL_FAVORITES, id);
			if (since_id != null && since_id != '')
				paras.push(new HttpParameter('since_id', since_id));
			if (count != null && count != '')
				paras.push(new HttpParameter('count', count));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.GET;
			
			var url:String = OauthUrlUtil.getOauthUrl(favURL, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			send(url, resultHandler, errorHandler);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_FAVORITES, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_FAVORITES, params);
			}
		}
		
		/**
		 * 添加收藏
		 * 
		 * @param id 必选参数，要收藏的微博ID
		 */		
		public function addFavorite(id:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_ADD_FAVORITES, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_ADD_FAVORITES, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_ADD_FAVORITES, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_ADD_FAVORITES, params);
			}
		}
		
		/**
		 * 删除收藏
		 * 
		 * @param id 必选参数，要收藏的微博ID
		 */		
		public function destroyFavorite(id:String):void
		{
			var paras:Array = [];
			if (id != null)
				paras.push(new HttpParameter('id', id));
			
			Oauth.currentAccountKey.httpMethod = URLRequestMethod.POST;
			
			paras = OauthUrlUtil.getOauthPostParas(URL_DESTROY_FAVORITES, 
				Oauth.currentAccountKey.httpMethod, 
				Oauth.currentAccountKey.consumerKey, 
				Oauth.currentAccountKey.consumerSecret,
				Oauth.currentAccountKey.tokenKey,
				Oauth.currentAccountKey.tokenSecrect,
				Oauth.currentAccountKey.verify,
				Oauth.currentAccountKey.callbackUrl,
				paras);
			
			var postParam:String = OauthUrlUtil.normalizeRequestParameters(paras);
			
			send(URL_DESTROY_FAVORITES, 
				resultHandler, 
				errorHandler, 
				URLRequestMethod.POST,
				postParam);
			
			function resultHandler(params:String):void
			{
				resultResponse(CMD_DESTROY_FAVORITES, params);
			}
			
			function errorHandler(params:String):void
			{
				errorResponse(CMD_DESTROY_FAVORITES, params);
			}
		}
		
		private function resultResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_dataHandler(cmd, data);
		}
		
		private function errorResponse(cmd:String, params:String):void
		{
			var data:Object = JSON.decode(params);
			_errorHandler(cmd, data);
		}
	}
}